$(document).ready(function(){

	access_token = $.getCookie("access_token");
	var sortTime = 0;
	var isLoad = 0;

	$.boundShowFunction = function(className,className2){
		$(className).click(function(){
			if($(className2).css('display') == "none"){
				$(className2).show();
				$(className+' .record-show').attr('src','/static/images/down-icon.png');
			}
			else{
				$(className2).hide();	
				$(className+' .record-show').attr('src','/static/images/up-icon.png');
			}
		})
	}

	$.loadMessage = function(){
		// alert(sortTime)
		$.getJSON("/"+server+"/checkHistoryGet?&access_token="+access_token+"&type=5&sortTime="+sortTime+requestLast,function(json){
			sortTime = json.sortTime;
			// alert(json.sortTime)
			
			if(json.result == 1){
				$('.loading-more').hide();
				for(var i=0;i<json.data.length;i++){
					var titleName = "record-title"+json.data[i].id;
					var contentName = "record-content"+json.data[i].id;
					var flowImage = "";
					var overtimeMoney = "";
					var titleType = "";
					var borrowAndReturnTime = "";
					var borrowAndReturnTimeStr = "";
					var titleStyleStr = "";

					if(json.data[i].type == 1){
						titleType = "正在使用";
						overtimeMoney = json.data[i].overtimeMoney;		
						borrowAndReturnTimeStr = "借入时间";
						borrowAndReturnTime = json.data[i].addTime;
						titleStyleStr = 'style="color:#00CEFC"';
					}
					else{
						titleType = "已经归还";	
						overtimeMoney = json.data[i].factOvertimeMoney;	
						borrowAndReturnTimeStr = "归还时间";
						borrowAndReturnTime = json.data[i].returnTime;											
					}


					$("body").append(			
							'<div class="record">'+
								'<div class="record-title '+titleName+'" '+titleStyleStr+'>'+
									'<span class="record-time">'+json.data[i].addTime+'</span>'+
									'<span class="record-type">'+titleType+'</span>'+
									'<img class="record-show" src="/static/images/up-icon.png">'+
								'</div>'+
								'<div class="record-content '+contentName+'" style="display:none">'+
									  '<div class="flow flow1">'+
											'<p>'+borrowAndReturnTimeStr+'</p>'+
											'<span>'+borrowAndReturnTime+'</span>'+
									  '</div>'+
									  '<div class="flow flow2">'+
											'<p>充电宝ID</p>'+
											'<span>'+json.data[i].cdbSimple+'</span>'+
									  '</div>'+
									  '<div class="flow flow3">'+
											'<p>产生费用</p>'+
											'<span>'+overtimeMoney+'元</span>'+
									  '</div>'+	
								'</div>'+
							'</div>'
						)	
					$.boundShowFunction('.'+titleName,'.'+contentName);	
					isLoad = 0;		
				}
			}
			if(sortTime == -1){
				$("body").append(
					'<div class="no-data" style="text-align:center;font-size:1em">'+
						'没有更多了！'+
					'</div>'
				)
			}
		});	
	}

	$.loadMessage();

	$(window).scroll(function() {
		if(sortTime != -1 && isLoad == 0){
			isLoad = 1;
			if($(document).scrollTop() + $(window).height() >= $(document).height()){
				$('.loading-more').show();
				$.loadMessage();
			}
		}
	});

})